<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : !disabled ? '修改' : '查看'"
    :close-on-click-modal="false"
    :lock-scroll="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="120px">
      <el-form-item label="USER" prop="user">
        <el-input v-model="dataForm.user" :disabled="disabled" placeholder="USER"></el-input>
      </el-form-item>
      <el-form-item label="UKEY" prop="ukey">
        <el-input v-model="dataForm.ukey" :disabled="disabled" placeholder="UKEY"></el-input>
      </el-form-item>
      <el-form-item label="打印机名称" prop="name">
        <el-input v-model="dataForm.name" :disabled="disabled" placeholder="打印机名称"></el-input>
      </el-form-item>
      <el-form-item label="打印机编号" prop="sn">
        <el-input v-model="dataForm.sn" :disabled="disabled" placeholder="打印机编号"></el-input>
      </el-form-item>
      <el-form-item label="所属门店" prop="shopsId">
        <el-select v-model="dataForm.shopsId" :disabled="disabled" clearable filterable placeholder="所属门店">
          <el-option
            v-for="shops in shopsList"
            :key="shops.id"
            :label="shops.name"
            :value="shops.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="存根打印机" prop="stubSn">
        <el-input v-model="dataForm.stubSn" :disabled="disabled" placeholder="存根打印机"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button v-if="!disabled" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      disabled: false,
      visible: false,
      dataForm: {
        id: 0,
        user: '',
        ukey: '',
        name: '',
        sn: '',
        shopsId: '',
        stubSn: ''
      },
      shopsList: [],
      dataRule: {
        user: [
          {
            required: true,
            message: 'USER不能为空',
            trigger: 'blur'
          }
        ],
        ukey: [
          {
            required: true,
            message: 'UKEY不能为空',
            trigger: 'blur'
          }
        ],
        name: [
          {
            required: true,
            message: '名称不能为空',
            trigger: 'blur'
          }
        ],
        sn: [
          {
            required: true,
            message: '打印机编号不能为空',
            trigger: 'blur'
          }
        ],
        shopsId: [
          {
            required: true,
            message: '所属门店不能为空',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    init (id, disabled) {
      this.disabled = disabled
      this.dataForm.id = id || ''
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: `/sys/printer/info/${this.dataForm.id}`,
            method: 'get'
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm = data.printer
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm']
        .validate((valid) => {
          if (valid) {
            this.$http({
              url: `/sys/printer/${!this.dataForm.id ? 'save' : 'update'}`,
              method: 'post',
              data: this.dataForm
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500
                })
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          }
        })
    }
  },
  created () {
    this.$http({
      url: '/mall/shops/queryAll',
      method: 'get'
    }).then(({data}) => {
      if (data && data.code === 0) {
        this.shopsList = data.list
      }
    })
  }
}
</script>
